package com.xj.qq.fragments;


import android.app.Fragment;
import android.os.Bundle;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;

import com.xj.qq.R;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */


public class ListFragment extends Fragment {

    private ExpandableListView expandableListView;
    private List<String> Group;
    private List<List<String>> Child;
    private List<String> Child_One;
    private List<String> Child_Two;
    private List<String> Child_Three;
    private List<String> Child_Four;
    private List<String> Child_Five;
    private List<List<Integer>> ChildPicture;
    public ListFragment() {
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.list_fragment, container, false);
        Group = new ArrayList<String>();
        Group.add("宫");
        Group.add("商");
        Group.add("角");
        Group.add("徵");
        Group.add("羽");
        Child_One = new ArrayList<String>();
        Child_One.add("好友1号");

        Child_Two= new ArrayList<String>();
        Child_Two.add("好友2号");
        Child_Two.add("好友8号");
        Child_Two.add("好友12号");

        Child_Three= new ArrayList<String>();
        Child_Three.add("好友3号");
        Child_Three.add("好友7号");
        Child_Three.add("好友9号");
        Child_Three.add("好友11号");
        Child_Three.add("好友13号");

        Child_Four= new ArrayList<String>();
        Child_Four.add("好友4号");
        Child_Four.add("好友6号");
        Child_Four.add("好友10号");

        Child_Five= new ArrayList<String>();
        Child_Five.add("好友5号");

        Child = new ArrayList<List<String>>();
        Child.add(Child_One);
        Child.add(Child_Two);
        Child.add(Child_Three);
        Child.add(Child_Four);
        Child.add(Child_Five);

        expandableListView = (ExpandableListView)view.findViewById(R.id.lists);
        expandableListView.setAdapter(new MyExpandableListViewAdapter(getActivity()));
        expandableListView.setGroupIndicator(null);
        return view;
    }

    class MyExpandableListViewAdapter extends BaseExpandableListAdapter {
        private Context context;

        public MyExpandableListViewAdapter(Context context) {
            this.context = context;
        }

        @Override
        public int getGroupCount() {
            return Group.size();
        }

        @Override
        public int getChildrenCount(int groupPosition) {
            return Child.get(groupPosition).size();
        }

        @Override
        public Object getGroup(int groupPosition) {
            return Group.get(groupPosition);
        }

        @Override
        public Object getChild(int groupPosition, int childPosition) {
            return Child.get(groupPosition).get(childPosition);
        }

        @Override
        public long getGroupId(int groupPosition) {
            return groupPosition;
        }

        @Override
        public long getChildId(int groupPosition, int childPosition) {
            return childPosition;
        }

        @Override
        public boolean hasStableIds() {
            return true;
        }

        @Override
        public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
            GroupHolder groupHolder = null;
            if (convertView == null) {
                convertView = (View) getActivity().getLayoutInflater().from(context).inflate(R.layout.groups, null);
                groupHolder = new GroupHolder();
                groupHolder.txt = (TextView) convertView.findViewById(R.id.textGroup);
                convertView.setTag(groupHolder);
            } else {
                groupHolder = (GroupHolder) convertView.getTag();
            }
            groupHolder.txt.setText(Group.get(groupPosition));
            return convertView;
        }

        @Override
        public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
            ItemHolder itemHolder = null;
            if (convertView == null) {
                convertView = (View) getActivity().getLayoutInflater().from(context).inflate(R.layout.childs, null);
                itemHolder = new ItemHolder();
                itemHolder.txt = (TextView) convertView.findViewById(R.id.textChild);
                convertView.setTag(itemHolder);
            } else {
                itemHolder = (ItemHolder) convertView.getTag();
            }
            itemHolder.txt.setText(Child.get(groupPosition).get(childPosition));
            return convertView;
        }

        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return true;
        }

        class GroupHolder {
            public TextView txt;
        }

        class ItemHolder {
            public TextView txt;
        }
    }
}
